﻿using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Input;

namespace TestRunner 
{
    public class GenericCommand: ICommand
    {
        private readonly Func<bool> canExecute;
        private readonly Action execute;

        internal GenericCommand(Action execute, Func<bool> canExecute)
        {
            this.canExecute = canExecute;
            this.execute = execute;
        }

        #region ICommand Members

        public bool CanExecute(object parameter) 
        {
            if (canExecute != null)
            {
                return canExecute();
            }
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            execute();
        }
        public void NotifyStateChange()
        {
            var handler = CanExecuteChanged;
            if (handler != null) handler(this, EventArgs.Empty);
        }
        #endregion
    }

    public class GenericCommand<T>: ICommand
    {
        private readonly Func<bool> canExecute;
        private readonly Action<T> execute;

        internal GenericCommand(Action<T> execute, Func<bool> canExecute)
        {
            this.canExecute = canExecute;
            this.execute = execute;
        }

        #region ICommand Members

        public bool CanExecute(object parameter) 
        {
            if (canExecute != null)
            {
                return canExecute();
            }
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            execute((T)parameter);
        }

        #endregion
    }

    public class OpenFileCommand: ICommand
    {
        #region ICommand Members

        public bool CanExecute(object parameter)
        {
            string path = parameter as string;
            if(!String.IsNullOrWhiteSpace(path) && File.Exists(path))
            {
                return true;
            }
            return false;
        }
        
        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            string path = parameter as string;
            if(!String.IsNullOrWhiteSpace(path) && File.Exists(path))
            {
                try
                {
                    Process.Start(path);
                }
                catch (Exception)
                {
                    
                }
            }
        }

        #endregion
    }

}
